***********************************************************************************
***	Replication file for:                                                     	***
*** Braun, S. T. and Stuhler, J. (2024). The Economic Consequences of 			***
***	Being Widowed by War: A Life-Cycle Perspective.	     						***
***																				***
***	Journal of Public Economics                                                 ***
***   							                                                ***
*** Script:		_1-ghs-1-prep-life-cycle.do					   			 		***	
*** Purpose:	data preparation, life-cycle data GHS-1							***
***																				***
***********************************************************************************


**************************************************
* RECORD 2 TRENNVA – Trennung vom Vater
* RECORD 2 TRENNVA - Separation from the father
**************************************************

use "$widowsghs/data/LV1/TRENNVA.DTA", clear

*** Beginning and end of spell

rename CB02 spell_beginn
rename CB03 spell_ende

*** Recode missings

recode B02 97/99 = .
recode B03 97/99 = .
recode B04 97/99 = .
recode B05 97/99 = .

*** Generate dummies that indicate whether the respective spell is in the respective month (t=349...1000) 
*** t are months since 1900
*** Or in other words: whether there is a separation in the respective month 

forvalues t = 349/1000 {
gen trennva__`t'=1 if spell_beginn <= `t' & spell_ende >= `t'
egen trennva_`t'=max(trennva__`t'), by(A01)
}

*** Reason 1 for absence

forvalues t = 349/1000 {
gen B04__`t'=.
replace B04__`t'=B04 if trennva__`t'==1
egen B04_`t'=max(B04__`t'), by(A01)
}
drop B04__*

*** Reason 2 for absence

forvalues t = 349/1000 {
gen B05__`t'=.
replace B05__`t'=B05 if trennva__`t'==1
egen B05_`t'=max(B05__`t'), by(A01)
}
drop B05__*

drop trennva__*

*** Keep only one episode per person
keep if B01==1
keep A01 trennva* B04_* B05_*

*** Create panel dataset
reshape long trennva_ B04_ B05_, i(A01) j(t)

rename trennva_ trennva_t
label var trennva_t "Was the person separated from the father at time t?"

rename B04_ grund1_trennva_t
label var grund1_trennva_t "Reason 1 for separation from the father at time t"

rename B05_ grund2_trennva_t
label var grund2_trennva_t "Reason 2 for separation from the father at time t"

save "$widowsghs/processed/intermediate/LVI_2_TRENNVA_Panel.dta", replace

**************************************************
* RECORD 3 TRENNMA – Trennung von der Mutter
* RECORD 3 TRENNMA - Separation from the mother
**************************************************

use "$widowsghs/data/LV1/TRENNMA.DTA", clear

*** Beginning and end of spell

rename CC02 spell_beginn
rename CC03 spell_ende

*** Recode missings

recode C02 97/99 = .
recode C03 97/99 = .
recode C04 97/99 = .
recode C05 97/99 = .


*** Generate dummies that indicate whether the respective spell is in the respective month (t=349...1000)
*** Or in other words: whether there is a separation in the respective month 

forvalues t = 349/1000 {
gen trennma__`t'=1 if spell_beginn <= `t' & spell_ende >= `t'
egen trennma_`t'=max(trennma__`t'), by(A01)
}

*** Reason 1 for absence

forvalues t = 349/1000 {
gen C04__`t'=.
replace C04__`t'=C04 if trennma__`t'==1
egen C04_`t'=max(C04__`t'), by(A01)
}
drop C04__*

*** Reason 2 for absence

forvalues t = 349/1000 {
gen C05__`t'=.
replace C05__`t'=C05 if trennma__`t'==1
egen C05_`t'=max(C05__`t'), by(A01)
}
drop C05__*

drop trennma__*

*** Keep only one episode per person
keep if C01==1
keep A01 trennma* C04_* C05_*

*** Create panel dataset
reshape long trennma_ C04_ C05_, i(A01) j(t)

rename trennma_ trennma_t
label var trennma_t "Was the person separated from the mother at time t?"

rename C04_ grund1_trennma_t
label var grund1_trennma_t "Reason 1 for separation from the mother at time t"

rename C05_ grund2_trennma_t
label var grund2_trennma_t "Reason 2 for separation from the mother at time t"

save "$widowsghs/processed/intermediate/LVI_3_TRENNMA_Panel.dta", replace

**************************************************
* RECORD 5 WOHNUNG – Wohngeschichte
* RECORD 5 WOHNUNG - Living history
**************************************************

*** Not included in the dataset

**************************************************
* RECORD 6 SCHOOL – Ausbildungsgeschichte
* RECORD 6 SCHOOL - Education history
**************************************************

* NB: This spell data only records Ausbildung, not schooling (which is recorded in RECORD 1) 

use "$widowsghs/data/LV1/SCHOOL.DTA", clear

*** Recode missings

recode F06 997/999 = .
recode F07 7/9 = .

*** Beginning and end of spell

rename CF03 spell_beginn
rename CF05 spell_ende


*** Generate dummies that indicate whether the respective spell is in the respective month (t=349...1000)
forvalues t = 349/1000 {
gen ausbildung__`t'=1 if spell_beginn <= `t' & spell_ende >= `t'
egen ausbildung_`t'=max(ausbildung__`t'), by(A01)
}

*** Type of school / vocational training

forvalues t = 349/1000 {
gen F06__`t'=.
replace F06__`t'=F06 if ausbildung__`t'==1
egen F06_`t'=max(F06__`t'), by(A01)
}
drop F06__*

*** School / training qualification

forvalues t = 349/1000 {
gen F07__`t'=.
replace F07__`t'=F07 if ausbildung__`t'==1
egen F07_`t'=max(F07__`t'), by(A01)
}
drop F07__*

drop ausbildung__*

*** Keep only one episode per person
keep if F01==1
keep A01 ausbildung* F06_* F07_*

*** Create panel dataset
reshape long ausbildung_ F06_ F07_, i(A01) j(t)

rename ausbildung_ ausbildung_t
recode ausbildung_t . = 0
label var ausbildung_t "Was person in vocational training at time t?"

rename F06_ ausbildungstyp_t
label var ausbildungstyp_t "Type of training at time t"

rename F07_ ausbild_abschluss_t
label var ausbild_abschluss_t "Completion of the training attended at time t"

save "$widowsghs/processed/intermediate/LVI_6_SCHOOL_Panel.dta", replace

**************************************************
* RECORD 7 EIGJOB – Berufsgeschichte
* RECORD 7 EIGJOB - Career history
**************************************************

use "$widowsghs/data/LV1/EIGJOB.DTA", clear

*** Beginning and end of spell

rename CG08 spell_beginn
rename CG10 spell_ende

*** Recoding prestige variable
* Code Prestige as missing if occupation is missing, still in training or retired/pensioner
replace G04 = . if (G03 == 8 | G03 == 9 | G03 == 5 | G03 == 7) 
* Code Prestige as 0 if jobseeker or non-employed housewife
replace G04 = 0 if G03 == 3 | G03 == 6
* Replace prestige value of 0 with missing if profession is indeterminable or inadequately described
recode G04 0 = . if G03 == 4

*** Recode missings

recode G03 8/9 = .
recode G12 97/99 = .
recode G15 97/99 = .
recode G17 97/99 = .
recode G19 99997/99999 = .
replace G19 = . if G21 != 0 | G22 != 0 /* Only monthly net income in DM */
recode G20 99997/99999 = .
replace G20 = . if G23 != 0 | G24 != 0 /* Only monthly net income in DM */
recode G25 97/99 = .

*** Generate dummies that indicate whether the respective spell is in the respective month (t=349...1000)
*** Or in other words: whether there is gainful employment in the respective month

forvalues t = 349/1000 {
gen erwerbstaetig__`t'=1 if spell_beginn <= `t' & spell_ende >= `t'
egen erwerbstaetig_`t'=max(erwerbstaetig__`t'), by(A01)
}

* Spell number

forvalues t = 349/1000 {
gen G01__`t'=.
replace G01__`t'=G01 if erwerbstaetig__`t'==1
egen G01_`t'=max(G01__`t'), by(A01)
}
drop G01__*

* ISCO

forvalues t = 349/1000 {
gen G03__`t'=.
replace G03__`t'=G03 if erwerbstaetig__`t'==1
egen G03_`t'=max(G03__`t'), by(A01)
}
drop G03__*

* Prestige

forvalues t = 349/1000 {
gen G04__`t'=.
replace G04__`t'=G04 if erwerbstaetig__`t'==1
egen G04_`t'=max(G04__`t'), by(A01)
}
drop G04__*

forvalues t = 349/1000 {
gen G28__`t'=.
replace G28__`t'=G28 if erwerbstaetig__`t'==1
egen G28_`t'=max(G28__`t'), by(A01)
}
drop G28__*

* Branche

forvalues t = 349/1000 {
gen G12__`t'=.
replace G12__`t'=G12 if erwerbstaetig__`t'==1
egen G12_`t'=max(G12__`t'), by(A01)
}
drop G12__*

* Professional position

forvalues t = 349/1000 {
gen G15__`t'=.
replace G15__`t'=G15 if erwerbstaetig__`t'==1
egen G15_`t'=max(G15__`t'), by(A01)
}
drop G15__*

* Hours per week

forvalues t = 349/1000 {
gen G17__`t'=.
replace G17__`t'=G17 if erwerbstaetig__`t'==1
egen G17_`t'=max(G17__`t'), by(A01)
}
drop G17__*

* Initial earnings (at the beginning of the spell)
* Keep only net monthly wages

forvalues t = 349/1000 {
gen G19__`t'=.
replace G19__`t'=G19 if erwerbstaetig__`t'==1
egen G19_`t'=max(G19__`t'), by(A01)
}
drop G19__*

* Final earnings (at the end of the spell)
forvalues t = 349/1000 {
gen G20__`t'=.
replace G20__`t'=G20 if erwerbstaetig__`t'==1
egen G20_`t'=max(G20__`t'), by(A01)
}
drop G20__*

* First reason for change of job
forvalues t = 349/1000 {
gen G25__`t'=.
replace G25__`t'=G25 if erwerbstaetig__`t'==1
egen G25_`t'=max(G25__`t'), by(A01)
}
drop G25__*


drop erwerbstaetig__*

*** Keep only one episode per person
keep if G01==1
keep A01 erwerbstaetig_* G01_* G03_* G04_* G12_* G15_* G17_* G19_* G20_* G25_* G28_*


*** Create panel dataset
reshape long erwerbstaetig_ G01_ G03_ G04_ G12_ G15_ G17_ G19_ G20_ G25_ G28_, i(A01) j(t)

rename erwerbstaetig_ erwerbstaetig_t
label var erwerbstaetig_t "Was the person employed at time t?"

rename G01_ spell_no_t
label var spell_no_t "Spell number at time t"

rename G03_ ISCO_beruf_t
label var ISCO_beruf_t "G03: ISCO code (3 digits) that the person exercised at time t"

rename G04_ prestige_beruf_trei_t
label var prestige_beruf_trei_t "G04: Prestige of the occupation held by the person at time t (Treiman)"

rename G28_ prestige_beruf_weg_t
label var prestige_beruf_weg_t "G28: Prestige of the occupation held by the person at time t (Wegener)"

rename G12_ branche_beruf_t
label var branche_beruf_t "G12: Sector of the profession that the person practiced at time t"

rename G15_ beruf_stellung_t
label var beruf_stellung_t "G15: Occupational status in the occupation held by the person at time t"

rename G17_ wochenstunden_beruf_t 
label var wochenstunden_beruf_t "Weekly working time at time t"

rename G19_ anfangsverdienst_t
label var anfangsverdienst_t "Monthly net income at the beginning of the employment spell"

rename G20_ endverdienst_t
label var endverdienst_t "Monthly net income at the end of the employment spell"

rename G25_ grund_taet_wechsel_t
label var grund_taet_wechsel_t "Reason 1 for changing jobs"

save "$widowsghs/processed/intermediate/LVI_7_EIGJOB_Panel.dta", replace

**************************************************
* RECORD 8 NEBENJOB – Nebentätigkeiten
* RECORD 8 NEBENJOB - Secondary employment
**************************************************

*** Not included in the dataset

**************************************************
* RECORD 9 FORTBILD – Fortbildungen
* RECORD 9 FORTBILD - Training courses
**************************************************

*** Not included in the dataset

**************************************************
* RECORD 10 LUECK1 – 
* Unterbrechungen nicht-institutionalisierter Akt.
* (Phase 1: Schulische/Berufliche Ausbildungen)
* Interruptions of non-institutionalized actv. 
* (phase 1: school/vocational training)
**************************************************

use "$widowsghs/data/LV1/LUECK1.DTA", clear

*** Beginning and end of spell
rename CK03 spell_beginn
rename CK05 spell_ende

*** Generate dummies that indicate whether the respective spell is in the respective month (t=349...1000)
forvalues t = 349/1000 {
gen unterbrechung_p1__`t'=1 if spell_beginn <= `t' & spell_ende >= `t'
egen unterbrechung_p1_`t'=max(unterbrechung_p1__`t'), by(A01)
}

* Activity 
forvalues t = 349/1000 {
gen K06__`t'=.
replace K06__`t'=K06 if unterbrechung_p1__`t'==1
egen K06_`t'=max(K06__`t'), by(A01)
}
drop K06__*
drop unterbrechung_p1__*

*** Keep only one episode per person
keep if K01==1
keep A01 unterbrechung_p1_* K06_*

*** Create panel dataset
reshape long unterbrechung_p1_ K06_, i(A01) j(t)

rename unterbrechung_p1_ unterbrechung_p1_t
label var unterbrechung_p1_t "Interruption in phase 1 (during school/training) at time t?"

rename K06_ aktivitaet_luecke_p1_t 
label var aktivitaet_luecke_p1_t "K06: Activity during interruption in phase 1 (during school/training) at time t"

save "$widowsghs/processed/intermediate/LVI_10_LUECK1_Panel.dta", replace

**************************************************
* RECORD 11 LUECK2 – 
* Unterbrechungen nicht-institutionalisierter Akt.
* Ab Eintritt Erwerbsleben
* Interruptions of non-institutionalized actv. 
* after entry into working life
**************************************************

use "$widowsghs/data/LV1/LUECK2.DTA", clear

*** Beginning and end of spell
rename CL03 spell_beginn
rename CL05 spell_ende

*** Generate dummies that indicate whether the respective spell is in the respective month (t=349...1000)
forvalues t = 349/1000 {
gen unterbrechung_p2__`t'=1 if spell_beginn <= `t' & spell_ende >= `t'
egen unterbrechung_p2_`t'=max(unterbrechung_p2__`t'), by(A01)
}

* Activity
forvalues t = 349/1000 {
gen L06__`t'=.
replace L06__`t'=L06 if unterbrechung_p2__`t'==1
egen L06_`t'=max(L06__`t'), by(A01)
}
drop L06__*
drop unterbrechung_p2__*

*** Keep only one episode per person
keep if L01==1
keep A01 unterbrechung_p2_* L06_*

*** Create panel dataset
reshape long unterbrechung_p2_ L06_, i(A01) j(t)

rename unterbrechung_p2_ unterbrechung_p2_t
label var unterbrechung_p2_t "Interruption in phase 2 (after starting working life) at time t?"

rename L06_ aktivitaet_luecke_p2_t
label var aktivitaet_luecke_p2_t "L06: Activity during interruption in phase 2 (after entering working life) at time t"

save "$widowsghs/processed/intermediate/LVI_11_LUECK2_Panel.dta", replace

**************************************************
* RECORD 12 KRANK – Krankheitsgeschichte
* RECORD 12 KRANK - Medical history
**************************************************

use "$widowsghs/data/LV1/REC1.DTA", clear
keep A01 A83 A84 A333 

merge 1:m A01 using "$widowsghs/Data/LV1/KRANK.DTA"
keep if _merge == 3
drop _merge

*** Recode missings
recode M02 9997/9999 = .
recode M03 97/99 = .

*** Beginning and end of spell
rename CM04 spell_beginn
rename CM05 spell_ende

* Recode spell end to date of interview if spell_ende == 1158 ("bis heute")
* Construct interview date in month since 1900
recode A83 96/99 = .
replace A83 = A83 - 20 if (A83 >= 21 & A83 <= 32)
recode A84 96/99 = .
gen time_interview = A84 * 12 + A83

replace spell_ende = time_interview if spell_ende == 1158

* Recode spell beginning to date of birth if spell_start == 1158 ("seit Geburt")
replace spell_beginn = A333 if spell_beginn == 1158

*** Spell Variable
*** Generate dummies that indicate whether the respective spell is in the respective month (t=349...1000)
forvalues t = 349/1000 {
gen illness__`t'=1 if spell_beginn <= `t' & spell_ende >= `t'
egen illness_`t'=max(illness__`t'), by(A01)
}

* Type of suffering - body

forvalues t = 349/1000 {
gen M02__`t'=.
replace M02__`t'=M02 if illness__`t'==1
egen M02_`t'=max(M02__`t'), by(A01)
}
drop M02__*

* Type of suffering - function

forvalues t = 349/1000 {
gen M03__`t'=.
replace M03__`t'=M03 if illness__`t'==1
egen M03_`t'=max(M03__`t'), by(A01)
}
drop M03__*


drop illness__*


*** Keep only one episode per person
keep if M01==1
keep A01 illness_* M02_* M03_* 

*** Create panel dataset
reshape long illness_ M02_ M03_, i(A01) j(t)

rename illness_ illness_t
label var illness_t "Illness at time t?"

rename M02_ leiden_koeper_t
label var leiden_koeper_t "M02: Type of suffering (body) during illness at time t"

rename M03_ leiden_funktion_t
label var leiden_funktion_t "M02: Type of suffering (function) during illness at time t"

save "$widowsghs/processed/intermediate/LVI_12_KRANK_Panel.dta", replace


**************************************************
* RECORD 13 JOBPART – Erwerbsgeschichte Partner
* RECORD 13 JOBPART - Employment history partner
**************************************************

*** Not included in the dataset

**************************************************
* RECORD 14 KINDER – Kinder
* RECORD 14 KINDER – Children
**************************************************

* Note: Here we only record the date of birth of the child in the cross-section and convert this into longitudinal information after merging

use "$widowsghs/data/LV1/KINDER.DTA", clear

keep A01 P01 CP04

rename CP04 geburt_monaten

reshape wide geburt_monaten, i(A01) j(P01)

save "$widowsghs/processed/intermediate/LVI_14_KINDER_Panel.dta", replace

**************************************************
* Querschnittsdaten zur Indexperson
* Cross-sectional data for index person
**************************************************

use "$widowsghs/data/LV1/REC1.DTA", clear


*** Schooling history

tab A04
recode A04 6/9 = .
recode A04 0 = .
rename A04 einschulung_jahreszeit
label var einschulung_jahreszeit "School enrollment season"

tab A05 
recode A05 96/99 = .
recode A05 0 = .
rename A05 einschulung_jahr
replace einschulung_jahr = 1900 + einschulung_jahr
label var einschulung_jahr "Year of school enrollment"

tab A21
recode A21 96/99 = .
recode A21 0 = .
rename A21 schulaustritt_monat 
replace schulaustritt_monat = schulaustritt_monat - 20 if (schulaustritt_monat >= 21 & schulaustritt_monat <= 32)
label var schulaustritt_monat "Month of leaving school"

tab A22
recode A22 96/99 = .
recode A22 0 = .
rename A22 schulaustritt_jahr
replace schulaustritt_jahr = 1900 + schulaustritt_jahr
label var schulaustritt_jahr "Year of leaving school" 

*** Military / civil service

tab A27
recode A27 6/9 = .
rename A27 militär_zivil_dienst
label var militär_zivil_dienst "Did military or civilian service?"

tab A29
recode A29 96/99 = .
rename A29 dienst_beginn_monat
replace dienst_beginn_monat = dienst_beginn_monat - 20 if (dienst_beginn_monat>= 21 & dienst_beginn_monat <= 32)
label var dienst_beginn_monat "Start of military or civilian service, month"

tab A30
recode A30 96/99 = .
recode A30 0 = .
rename A30 dienst_beginn_jahr
replace dienst_beginn_jahr = 1900 + dienst_beginn_jahr 
label var dienst_beginn_jahr "Start of military or civilian service, year"


tab A31
recode A31 96/99 = .
rename A31 dienst_ende_monat
replace dienst_ende_monat = dienst_ende_monat - 20 if (dienst_ende_monat>= 21 & dienst_ende_monat <= 32)
label var dienst_ende_monat "End of military or civilian service, month"

tab A32
recode A32 96/99 = .
recode A32 0 = .
rename A32 dienst_ende_jahr
replace dienst_ende_jahr = 1900 + dienst_ende_jahr 
label var dienst_ende_jahr "End of military or civilian service, year"


*** Socio-demographic characteristics

tab A140
recode A140 96/99 = .
rename A140 geburtsmonat
label var geburtsmonat "Month of birth"

tab A141
recode A141 96/99 = .
rename A141 geburtsjahr
replace geburtsjahr = geburtsjahr + 1900
label var geburtsjahr "Year of birth"

tab A206
recode A206 6/9 = .
rename A206 geschlecht
label var geschlecht "Gender"

tab A207
recode A207 96/99 = .
rename A207 familienstand
label var familienstand "Marital status"

tab A333
rename A333 geburtsdatum_monate
label var geburtsdatum_monate "Date of birth in months since 1900"

*** Generationindex 

gen index = 2001
label var index "Generationenindex"

keep A01 geburt* geschlecht familienstand index einschulung* schulaustritt* militär* dienst* 

save "$widowsghs/processed/intermediate/LVI_Querschnitt_IP_Panel.dta", replace

**************************************************
* Querschnittsdaten zum Partner der IP
**************************************************

*** Not included in the dataset


*************************************************************************
*** Creating the complete panel data set
*************************************************************************

use "$widowsghs/processed/intermediate/LVI_2_TRENNVA_Panel.dta", clear
merge 1:1 A01 t using "$widowsghs/processed/intermediate/LVI_3_TRENNMA_Panel.dta"
drop _merge
merge 1:1 A01 t using "$widowsghs/processed/intermediate/LVI_6_SCHOOL_Panel.dta"
drop _merge
merge 1:1 A01 t using "$widowsghs/processed/intermediate/LVI_7_EIGJOB_Panel.dta"
drop _merge
merge m:m A01 t using "$widowsghs/processed/intermediate/LVI_10_LUECK1_Panel.dta"
drop _merge
merge m:m A01 t using "$widowsghs/processed/intermediate/LVI_11_LUECK2_Panel.dta"
drop _merge
merge m:m A01 t using "$widowsghs/processed/intermediate/LVI_12_KRANK_Panel.dta"
drop _merge
merge m:m A01 using "$widowsghs/processed/intermediate/LVI_Querschnitt_IP_Panel.dta"
drop _merge


label var t "Time in months since 1900 (e.g. March 1950 = 50*12+3 = 603)"

gen jahr = 1900 + floor((t-1)/12)
label var jahr "Year"

gen monat = t - (jahr-1900)*12
label var monat "Month"

gen alter_t = t - geburtsdatum_monate
replace alter_t = . if alter_t < 0 
label var alter_t "Age at time t in months"

gen alter_j = jahr - geburtsjahr
replace alter_j = . if alter_j < 0  
label var alter_j "Age in year j in years"

* Number of children over time
* (Note: later deaths are not considered)

merge m:1 A01 using "$widowsghs/processed/intermediate/LVI_14_KINDER_Panel.dta"
drop _merge

gen 	anzahl_kinder_t = 0
replace anzahl_kinder_t = 1 if (geburt_monaten1 <= t & geburt_monaten1 != .)
replace anzahl_kinder_t = 2 if (geburt_monaten2 <= t & geburt_monaten2 != .)
replace anzahl_kinder_t = 3 if (geburt_monaten3 <= t & geburt_monaten3 != .)
replace anzahl_kinder_t = 4 if (geburt_monaten4 <= t & geburt_monaten4 != .)
replace anzahl_kinder_t = 5 if (geburt_monaten5 <= t & geburt_monaten5 != .)
replace anzahl_kinder_t = 6 if (geburt_monaten6 <= t & geburt_monaten6 != .)
replace anzahl_kinder_t = 7 if (geburt_monaten7 <= t & geburt_monaten7 != .)
replace anzahl_kinder_t = 8 if (geburt_monaten8 <= t & geburt_monaten8 != .)

drop geburt_monate*

label var anzahl_kinder_t "Number of children at time t"

compress

tsset A01 t

rename A01 fid
label var fid "Family ID"

gen lv1 = 1

compress

sort fid t

tsset fid t

save "$widowsghs/processed/intermediate/LVI_PANELDATEN_Index.dta", replace


*** Order variables 

use "$widowsghs/processed/intermediate/LVI_PANELDATEN_Index.dta", clear

* Panel IDs

gen ___Panel_Data______________ = .
gen ___Panel_IDs________________ = .

order ___Panel_Data______________ ___Panel_IDs________________ fid t monat jahr, first

* Separation from father/mother

gen ___Separation_Fa_Ma_______ = .
order ___Separation_Fa_Ma_______ trennva_t grund1_trennva_t grund2_trennva_t trennma_t grund1_trennma_t grund2_trennma_t, after(jahr)

* Vocational training 

gen ___Voc_Training___________ = .
order ___Voc_Training___________ ausbildung_t ausbildungstyp_t ausbild_abschluss_t, after(grund2_trennma_t)

 
* Employment

gen ___Employment___________ = .
order ___Employment___________ erwerbstaetig_t spell_no_t ISCO_beruf_t prestige_beruf_trei_t prestige_beruf_weg_t branche_beruf_t ///
beruf_stellung_t wochenstunden_beruf_t anfangsverdienst_t endverdienst_t grund_taet_wechsel_t, after(ausbild_abschluss_t)

label var spell_no_t "Spell number of employment"

* Interruption of education and working life

gen ___Interruptions________ = .
order ___Interruptions________ unterbrechung_p1_t aktivitaet_luecke_p1_t unterbrechung_p2_t aktivitaet_luecke_p2_t, after(grund_taet_wechsel_t)  

* Illnesses

gen ___Illness________________ = .
order ___Illness________________ illness_t leiden_koeper_t leiden_funktion_t, after(aktivitaet_luecke_p2_t)

* Schooling 
* (Interruptions are not recorded here)

gen einschulung_t = (einschulung_jahr - 1900)*12
replace einschulung_t = einschulung_t + 3 if einschulung_jahreszeit == 1
replace einschulung_t = einschulung_t + 9 if einschulung_jahreszeit == 2
label var einschulung_t "School enrollment in months since 1900"

gen schulaustritt_t = (schulaustritt_jahr - 1900)*12 + schulaustritt_monat 
label var schulaustritt_t "School leaving in months since 1900"

gen schulzeit_t = 1 if (t >= einschulung_t & t <= schulaustritt_t)
label var schulzeit_t "Attending school at time t?"

drop einschulung_t einschulung_jahr schulaustritt_t schulaustritt_jahr schulaustritt_monat einschulung_jahreszeit

gen ___Schooling______________ = .
order ___Schooling______________ schulzeit_t, after(leiden_funktion_t)

* Military/civilian service

recode dienst_beginn_monat 0 = .
recode dienst_ende_monat 0 = .

gen dienst_beginn_t = (dienst_beginn_jahr-1900)*12 + dienst_beginn_monat
gen dienst_ende_t = (dienst_ende_jahr-1900)*12 + dienst_ende_monat

gen dienst_t = 1 if (t >= dienst_beginn_t & t <= dienst_ende_t)

drop dienst_beginn_monat dienst_beginn_jahr dienst_ende_monat dienst_ende_jahr dienst_beginn_t dienst_ende_t

gen ___Service_________________ = .
order ___Service_________________ dienst_t militär_zivil_dienst, after(schulzeit_t)

* Age 

gen ___Age___________________ = .
order ___Age___________________ alter_t alter_j, after(militär_zivil_dienst)

* Family

gen ___Family_________________ = .
order ___Family_________________ anzahl_kinder_t, after(alter_j)

* Delete cross-sectional data 

drop geburtsmonat geburtsjahr geschlecht familienstand geburtsdatum_monate index lv1

save "$widowsghs/processed/intermediate/LVI_PANELDATEN_Index_edt.dta", replace

*************************************************************************
*** Generate variables on sector and occupation
*************************************************************************

*** Load data

use "$widowsghs/processed/intermediate/LVI_PANELDATEN_Index_edt.dta", clear


*** Generate (more aggregated) variables for sector and occupation

/*
(0) UNZUTREFFEND ODER FALSCH BESCHRIEBEN
(1) LAND-,FORSTWIRTSCH.,FISCHEREI,TIERHALT.
(2) ENERGIEWIRTSCHAFT,WASSERVERSORG.,BERGBAU
(3) CHEMIE,STEINE,ERDEN,GLAS,GUMMI,ASBEST
(4) EISEN, STAHL, NE-METALLE
(5) STAHL-, MASCHINEN-, FAHRZEUGBAU
(6) EDV, BUEROMASCHINEN
(7) E-TECHNIK,METALLWA.,FEINMECH.,MUSIKINST.
(8) HOLZ,PAPIER,DRUCK-,TEXTIL-,LEDERGEWERBE
(9) NAHRUNGS- UND GENUSSMITTELGEWERBE
(10) HOCH-, TIEFBAU, AUSBAU-, BAUHILFSGEWERBE
(11) GROSS-, EINZELHANDEL, HANDELSVERMITTLUNG
(12) VERKEHR,NACHRICHTENUEBERMITTLUNG [BP+DB]
(13) BANKEN UND VERSICHERUNGEN
(14) HOTEL-, GASTSTAETTENGEWERBE
(15) REINIGUNG,SCHORNSTEINFEGER,KOERPERPFLEGE
(16) WISSENSCHAFT,BILDUNG,KUNST,PUBLIZISTIK
(17) GESUNDHEITSWESEN
(18) RECHTS-,STEUERWESEN,ING.BUERO,FOTOGRAFIE
(19) SONSTIGE PRIVATE DIENSTLEISTUNGEN
(20) KIRCHE,VERBAENDE,ORGANISATION,HAUSHALTE
(21) BEHOERDE,VERTEIDIGUNG,SOZIALVERSICHERUNG
*/

** Distinguish between three sectors

gen sector_3_t = .
replace sector_3_t = 1 if (branche_beruf_t == 1)
replace sector_3_t = 2 if (branche_beruf_t >= 2 & branche_beruf_t <= 10)
replace sector_3_t = 3 if (branche_beruf_t >= 11 & branche_beruf_t <= 21)

label define sector_3_label 1 "Primary sector" 2 "Secondary sector" 3 "Tertiary sector"
label values sector_3_t sector_3_label

label var sector_3_t "Sector of employment at time t, three sectors"

** Distinguish between eight sectors

gen sector_8_t = .
replace sector_8_t = 1 if (branche_beruf_t == 1)
replace sector_8_t = 2 if (branche_beruf_t >= 2 & branche_beruf_t <= 9)
replace sector_8_t = 3 if (branche_beruf_t == 10)
replace sector_8_t = 4 if (branche_beruf_t == 11)
replace sector_8_t = 5 if (branche_beruf_t == 12 |  branche_beruf_t == 14)
replace sector_8_t = 6 if (branche_beruf_t == 13)
replace sector_8_t = 7 if (branche_beruf_t >= 15 & branche_beruf_t <= 19)
replace sector_8_t = 8 if (branche_beruf_t >= 20 & branche_beruf_t <= 21)

label define sector_8_label 1 "Agriculture" 2 "Industry" 3 "Construction" 4 "Trade" 5 "Transport and tourism" 6 "Financial services" 7 "Other private services" 8 "Public services and organisations"																																												
label values sector_8_t sector_8_label

label var sector_8_t "Sector of employment at time t, eight sectors"

** Distinguish between seven types of occupational status

/*
(10) LANDWIRT UNTER 10 HA
(11) LANDWIRT 10 BIS 20 HA
(12) LANDWIRT 20 BIS 50 HA
(13) LANDWIRT 50 HA UND MEHR
(14) FREIBERUFLER (Z.B. MEDIZIN. HILFSBERUFE)
(15) AKADEMISCHER BERUF, 1 MITARBEITER ODER ALLEIN
(16) AKADEMISCHER BERUF, 2–9 MITARBEITER
(17) AKADEMISCHER BERUF, 10 UND MEHR MITARBEITER
(18) LANDWIRT MIT UNBEKANNTER NUTZFLÄCHE
(19) LANDWIRT OHNE NÄHERE ANGABEN
(20) HEIMARBEIT
(21) SELBSTÄNDIG, 1 MITARBEITER ODER ALLEIN
(22) SELBSTÄNDIG, 2–9 MITARBEITER
(23) SELBSTÄNDIG, 10–49 MITARBEITER
(24) SELBSTÄNDIG, 50 UND MEHR MITARBEITER
(28) SELBSTÄNDIG, NÄHERE DEFINITION NICHT GEWUSST
(29) SELBSTÄNDIG OHNE NÄHERE ANGABEN
(30) FAMILIENHILFE
(40) BEAMTE EINFACHER DIENST
(41) BEAMTE MITTLERER DIENST
(42) BEAMTE GEHOBENER DIENST
(43) BEAMTE HÖHERER DIENST
(48) BEAMTE MIT UNBEKANNTEM STATUS
(49) BEAMTE OHNE WEITERE ANGABEN
(50) WERKMEISTER
(51) ANGESTELLTE MIT EINFACHER TÄTIGKEIT
(52) ANGESTELLTE MIT SCHWERER TÄTIGKEIT
(53) ANGESTELLTE MIT VERANTWORTLICHER TÄTIGKEIT
(54) ANGESTELLTE MIT FÜHRENDER TÄTIGKEIT
(58) ANGESTELLTE MIT UNBEKANNTEM STATUS
(59) ANGESTELLTE OHNE WEITERE ANGABEN
(60) UNGELERNTE ARBEITER
(61) ANGELERNTE ARBEITER
(62) FACHARBEITER
(63) VORARBEITER
(64) MEISTER
(68) ARBEITER MIT UNBEKANNTEM STATUS
(69) ARBEITER OHNE WEITERE ANGABEN
(74) PRAKTIKANTEN/VOLONTÄRE
(75) PFLICHTJAHR
(76) HAUSHALTSJAHR/DIAKONISCHES JAHR
(82) AUSZUBILDENDE
(84) SOLDAT IM KRIEGSDIENST

(10) LANDWIRT 10 HA
(11) LANDWIRT 10-20 HA
(12) LANDWIRT 20-50 HA
(13) LANDWIRT 50
(15) AKADEMISCH 1HELFER
(16) AKADEMISCH 2-9
(17) AKADEMISCH 10
(21) SELBSTAENDIG 1HELFER
(22) SELBSTAENDIG 2-9
(23) SELBSTAENDIG 10-49
(24) SELBSTAENDIG 50
(30) FAMILIENHILFE
(40) BEAMTE EINFACH
(41) BEAMTE MITTEL
(42) BEAMTE GEHOBEN
(43) BEAMTE HOEHER
(49) WEHRPFLICHTIGER
(50) WERKMEISTER
(51) ANGESTELLT EINFACH
(52) ANGESTELLT 2
(53) ANGESTELLT 3
(54) ANGESTELLT FUEHREND
(60) UNGELERNT ARBEITER
(61) ANGELERNT ARBEITER
(62) FACHARBEITER
(63) VORARBEITER
(64) MEISTER
(74) VOLONTAIRE
(75) PFLICHTJAHR
(76) HAUSHALTSJAHR
(80) HAUSFRAU
(81) RENTNER
(82) IN AUSBILDUNG
(83) ARBEITSLOS


*/

gen 	occupation_7_t = 1 if (beruf_stellung_t >= 10 & beruf_stellung_t <= 13)
replace occupation_7_t = 2 if (beruf_stellung_t >= 15 & beruf_stellung_t <= 24)
replace occupation_7_t = 3 if (beruf_stellung_t == 30)
replace occupation_7_t = 4 if (beruf_stellung_t >= 40 & beruf_stellung_t <= 43)
replace occupation_7_t = 5 if (beruf_stellung_t >= 50 & beruf_stellung_t <= 54)
replace occupation_7_t = 6 if (beruf_stellung_t >= 60 & beruf_stellung_t <= 64)
replace occupation_7_t = 7 if (beruf_stellung_t >= 74 & beruf_stellung_t <= 76) | beruf_stellung_t == 49

label define occupation_7_label 1 "Farmer" 2 "Self-employed" 3 "Family co-worker" 4 "Civil servant" 5 "White collar worker" 6 "Blue collar worker" 7 "Apprentice or military service"
label values occupation_7_t occupation_7_label

label var occupation_7_t "Occupational status at time t"

order sector_3_t sector_8_t occupation_7_t, after(grund_taet_wechsel_t)

sort fid t 
compress
save "$widowsghs/processed/LVS1_panel_data_IP.dta", replace


*** EOF







